/*
 * Copyright (c) 2020 Fraunhofer FOKUS and others. All rights reserved.
 *
 * See the NOTICE file(s) distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contact: mosaic@fokus.fraunhofer.de
 */
package org.eclipse.mosaic.fed.carla.clojure.commands;

import libpython_clj2.java_api;
import org.eclipse.mosaic.fed.carla.clojure.AbstractCarlaCommand;
import org.eclipse.mosaic.fed.carla.clojure.ClojureCarlaConnection;

import java.util.Map;

public class TrafficLightStateTransform extends AbstractCarlaCommand {

    public TrafficLightStateTransform() {
        super();
    }

    public Object execute(ClojureCarlaConnection connection, Character sumoTlState) {
        try (AutoCloseable locker = java_api.GILLocker()) {
            Map global = java_api.runStringAsFile(
                    "\n" +
                            "import carla\n" +
                            "def get_carla_traffic_light_state(sumo_tl_state):\n" +
                            "        if sumo_tl_state == 'r' or sumo_tl_state == 'u':\n" +
                            "            return carla.TrafficLightState.Red\n" +
                            "\n" +
                            "        elif sumo_tl_state == 'y':\n" +
                            "            return carla.TrafficLightState.Yellow\n" +
                            "\n" +
                            "        elif sumo_tl_state == 'G' or \\\n" +
                            "             sumo_tl_state == 'g':\n" +
                            "            return carla.TrafficLightState.Green\n" +
                            "\n" +
                            "        elif sumo_tl_state == 'O':\n" +
                            "            return carla.TrafficLightState.Off\n" +
                            "\n" +
                            "        else: \n" +
                            "            return carla.TrafficLightState.Unknown"
            );
            Object getCarlaTrafficLightState = global.get("get_carla_traffic_light_state");
            return java_api.call(getCarlaTrafficLightState, sumoTlState);
        } catch (Exception e) {
            throw new RuntimeException("Traffic light transform failed when request carla. ", e);
        }
    }
}
